import 'package:flutter/material.dart';//新建dart文件页面需要导入的包
import 'package:flutter/services.dart';//消息通道需要导入的包
//可刷新数据的页面需要继承StatefulWidget
class OnePage extends StatefulWidget {
  @override
  _OnePageState createState() => _OnePageState();
}

class _OnePageState extends State<OnePage> {
  //注册信息通道
  static const BasicMessageChannel<String> platform =
  BasicMessageChannel<String>("channel_name", StringCodec());//注意参数要和activity中一致
  String message = "德玛西亚";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Sample App"),
        ),
        body: Center(child: Text(message))
    );
  }

  Future<String> _handleMessage(String message) async {
    setState(() {
      this.message = message;
    });
    return "";
  }

  //接收到Android activity发送到消息  处理消息
  @override
  void initState() {
    super.initState();
    platform.setMessageHandler(_handleMessage);
  }
}
